package com.example.cheeseerpadmin.bus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.cheeseerpadmin.bus.entity.*;
import com.example.cheeseerpadmin.bus.service.BusCustomerService;
import com.example.cheeseerpadmin.bus.service.BusGoodsService;
import com.example.cheeseerpadmin.bus.service.BusSalesbackService;
import com.example.cheeseerpadmin.bus.vo.SalesbackVo;
import com.example.cheeseerpadmin.sys.common.DataGridView;
import com.example.cheeseerpadmin.sys.common.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cheese
 * @since 2022-02-21
 */
@RestController
@RequestMapping("/salesback")
public class SalesbackController {

    @Autowired
    public BusSalesbackService busSalesbackService;
    @Autowired
    public BusCustomerService busCustomerService;
    @Autowired
    public BusGoodsService busGoodsService;

    /**
     * 增加退货信息
     *
     * @param id
     * @param number
     * @param remark
     * @return
     */
    @RequestMapping("/addSalesback")
    public ResultObj addSalesback(Integer id, Integer number, String remark) {
        try {
            busSalesbackService.addSalesback(id, number, remark);
            return ResultObj.OPERATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.OPERATE_ERROR;
        }
    }

    /**
     * 加载所有销售退货信息
     *
     * @param salesbackVo
     * @return
     */
    @RequestMapping("/loadAllSalesback")
    public DataGridView loadAllSalesback(SalesbackVo salesbackVo) {
        Page<BusSalesback> busSalesbackPage = new Page<>();
        QueryWrapper<BusSalesback> busSalesbackQueryWrapper = new QueryWrapper<>();
        busSalesbackQueryWrapper.eq(salesbackVo.getCustomerid() != null && salesbackVo.getCustomerid() != 0, "customerid", salesbackVo.getCustomerid());
        busSalesbackQueryWrapper.eq(salesbackVo.getGoodsid() != null && salesbackVo.getGoodsid() != 0, "goodsid", salesbackVo.getGoodsid());
        busSalesbackQueryWrapper.ge(salesbackVo.getStartDate() != null, "salesbacktime", salesbackVo.getStartDate());
        busSalesbackQueryWrapper.le(salesbackVo.getEndDate() != null, "salesbacktime", salesbackVo.getEndDate());
        busSalesbackQueryWrapper.like(StringUtils.isNotBlank(salesbackVo.getOperateperson()), "operateperson", salesbackVo.getOperateperson());
        busSalesbackQueryWrapper.like(StringUtils.isNotBlank(salesbackVo.getRemark()), "remark", salesbackVo.getRemark());
        busSalesbackQueryWrapper.orderByDesc("salesbacktime");
        Page<BusSalesback> page = busSalesbackService.page(busSalesbackPage, busSalesbackQueryWrapper);
        List<BusSalesback> list = page.getRecords();
        for (BusSalesback busSalesback : list) {
            Integer customerid = busSalesback.getCustomerid();
            BusCustomer busCustomer = busCustomerService.getById(customerid);
            if (busCustomer != null) {
                busSalesback.setCustomername(busCustomer.getCustomername());
            }
            Integer goodsid = busSalesback.getGoodsid();
            BusGoods busGoods = busGoodsService.getById(goodsid);
            if (busGoods != null) {
                busSalesback.setGoodsname(busGoods.getGoodsname());
                busSalesback.setSize(busGoods.getSize());
            }
        }
        return new DataGridView((long) list.size(), list);
    }
}

